//
//  AIGirlMessageNavView.swift
//  AIGirl
//
//  Created by Rae on 2023/12/29.
//

import UIKit

class AIGirlMessageNavView: UIView, AnimationProtocol, AnimationDelegate, AIGirlLottieAnimationProtocol {

    private lazy var backButton = c_make_ex(UIButton.self)
        .image("AIGirl_Message_back").end()
    private lazy var titleLabel = c_make_ex(UILabel.self)
        .color(.white).font(18.font_semiBold).end()
     var animateView = levelAnimateView()
    private lazy var settingButton = c_make_ex(UIButton.self)
        .image("AIGirl_Message_setting").end()
    private lazy var levelButton = c_make_ex(UIButton.self)
        .bgColor(.clear).end()
    
    var backClosure: (() -> Void)?
    var settingClosure: (() -> Void)?
    var levelClosure: (() -> Void)?
    
    var model: AIGirlChatRoomModel?{
        didSet{
            guard let md = model else { return }
            titleLabel.text = md.girlName
            animateView.type = md.level
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            backButton
            animateView
            titleLabel
            settingButton
            levelButton
        }
        
        backButton.leading(15).width(29).height(36).centerVertically()
        animateView.size(55).centerVertically().centerHorizontally(offset: -25)
        titleLabel.centerVertically().centerHorizontally(offset: 25)
        settingButton.trailing(15).size(25).centerVertically()
        levelButton.top(0).bottom(0)
        levelButton.Leading == animateView.Leading
        levelButton.Trailing == titleLabel.Trailing
        
        backButton.addTarget(self, action: #selector(pushToRootVC), for: .touchUpInside)
        settingButton.addTarget(self, action: #selector(pushToSettingVC), for: .touchUpInside)
        levelButton.addTarget(self, action: #selector(showLevelView), for: .touchUpInside)
        
        NotificationCenter.default.addObserver(forName: .updateLevel, object: nil, queue: nil) {  [weak self]  noti in
            let level = noti.object as? String ?? "1"
            self?.animateView.type = level
        }
    }
    
    @objc private func pushToRootVC(){
        backClosure?()
    }
    
    @objc private func pushToSettingVC(){
        settingClosure?()
    }
    
    @objc private func showLevelView(){
        levelClosure?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class levelAnimateView: UIView, AIGirlLottieAnimationProtocol{
    
    private lazy var animateView1: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_1)
    }()
    private lazy var animateView2: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_2)
    }()
    private lazy var animateView3: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_3)
    }()
    private lazy var animateView4: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_4)
    }()
    private lazy var animateView5: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_5)
    }()
    private lazy var animateView6: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_6)
    }()
    private lazy var animateView7: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_7)
    }()
    private lazy var animateView8: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_8)
    }()
    private lazy var animateView9: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_9)
    }()
    private lazy var animateView10: AIGirlLottieAnimationView = {
        lottieAnimation.make(.level_10)
    }()
    private lazy var subV: [AIGirlLottieAnimationView] = {
        var array = [AIGirlLottieAnimationView]()
        array.append(animateView1)
        array.append(animateView2)
        array.append(animateView3)
        array.append(animateView4)
        array.append(animateView5)
        array.append(animateView6)
        array.append(animateView7)
        array.append(animateView8)
        array.append(animateView9)
        array.append(animateView10)
        
        return array
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
    }
    
    var type: String = "1"{
        didSet {
            let intTy = (Int(type) ?? 1) - 1
            subviews{subV[intTy]}
            subV[intTy].fillContainer()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
